class LoginController < ApplicationController
  layout "application"
  def add_user
    @user = User.new(params[:user])
    if request.post? and @user.save
      flash.now[:notice] = "User #{@user.name} has been created"
      @user = User.new
    end
  end

  def login
    session[:user] = nil
    if request.post?
      user = User.authenticate(params[:name], params[:password])
      if user
        session[:user] = user        
        if(session[:intended_action] != nil and session[:intended_controller] != nil)
          redirect_to :action => session[:intended_action], :controller => session[:intended_controller]
        else
          if(user.role.code == 'Admin')
            redirect_to :action => 'list_users', :controller => 'login'
          else
            redirect_to :controller => 'clients'
          end
        end
      else
        flash[:notice] = "Invalid user/password combination"
      end
    end
  end

  def logout
    session[:user] = nil
    session[:intended_action] = nil
    session[:intended_controller] = nil    
    redirect_to :action => "login", :controller => "login"
  end

  def index
  end

  def list_users
    @users = User.find(:all)
  end

  def delete_user
  end

  def menu
    "{menu : [
       {label: 'Links',
            menu: [
                { label : 'Sun.com',
                  href : 'http://www.sun.com'},
                { label : 'jMaki.com',
                  href : 'http://www.jmaki.com'}
                ]
       },
       {label: 'Actions',
            menu: [
                { label : 'Select',
                  action :{topic: '/foo/select',
                         message: { targetId : 'bar'}}
                },
                { label :'Set Content',
                  action :{topic: '/foo/setContent',
                         message: { value : 'test.php'}}
                }
                ]}
                ]
       }"
  end
end
